<?php
namespace app\alipay\controller;

use alifacepay\f2fpay\model\builder\AlipayTradePrecreateContentBuilder;
use alifacepay\f2fpay\model\builder\ExtendParams;
use alifacepay\f2fpay\model\builder\GoodsDetail;
use alifacepay\f2fpay\service\AlipayTradeService;
use think\Controller;
use Endroid\QrCode\QrCode;

class Alifacepay extends Controller
{

    protected $config=[
        //签名方式,默认为RSA2(RSA2048)
        'sign_type' => "RSA2",
        //支付宝公钥
        'alipay_public_key' =>"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArBab5TafIQG7hkZo0hz7tiVkS7zDL1Ri7mNjmjeOw/wgUe/IcwwaWfBxk1zMOZMmVPC3Yzx/AF/PIkCZ5bCRSD11F4JD5KyYQy+KFQKuqNxNjGeGdkEuOd+wPsuHSoDPmCKUJc1ukQehq7j8xK1mb1SHhvPHy93psfmzI8Tsotiqh9f1jHbXciUiG8JVOB5Tu9a6kdKbKZenqbRG5wcK1ggHAm7/VMkaviYATziaxVBuzwLFrfkwRCbbctzaKhRPEgTF9mmanYg4EK3Id/8xBn8REkJJ/P2YgScx6q6UOYhv2QNe/vS1Mk/JQDUwC9wJLY5UZ+ITHKibhmjmHE5rgQIDAQAB",
        //商户私钥
        'merchant_private_key' =>"MIIEpQIBAAKCAQEAuOojDK5M8pd3tKyvXYOhCfv5aQn1SJ0Rx3ilFlC5eadqhyLdV+MT1YO0DRiHhb/lnYxIVQDL1mz36svR11n5ZjNp/Li4W82CuxhVtmMTVRpam7dn4k+eto9lA/3rAJSa6IYtOmiFfp7qvgKoIjNmjTLzU5f85ThRzJYxeRN40VzpXVqG7/RwYcrEeRe7FsznvegXYmg65nzyD2+ES0/hMxXxxEtbB7fJkI0rGlZLd8Z4mv8PK6mnjBDRZFU3NISAjdlo3Yx4cNUmzeONPpt3FQ4q+YO6V+logB6pIJQLcY5MpoPAbDms8gcuIwLbjfEXmsnS1puzJw8JHvZDsnUSiwIDAQABAoIBAFVNpbee3N54o39Uvl18cJjjha5AK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wN3wFwefwrbYirvSIlglUkLffBls9Jr//51K0n4OeW/DDy7Tk5YSDGGLlTffB1Zka1VlBl73fmNyeZYtD+ZCO++OTb/TGDYJ0sCgYB5u6DdLjj2ngNnMfsOk5EHwSyQ7p/UUThBeTrlqYc2BeldQwiHdQUT5VDXaduxEXyjrXNNfh61Wgx35wd344Y9Umyxu/q3B8n2toLwSmv6WdHxqgYh9pMaAW9hR2+cd+3fOUBa1yTRfIjmn5HGj81CByEKDsRPst7WyCICdRG1HwKBgHdXq0iH19cm5qADUjQCzAkkl90GHpjCijqyFkis1rLmQrH9ywCRYIqWBAZ7JwNxI4je5wtnrqM/AiwLzWfA6TKYO5JyX5ECxHWL9FtClC+7D4E+7810eT2EOP4WCcvVQelEzodN3PjeYzWW636H4816hDwcAJ74TSqpyyKC9YTZAoGAbqNPqxxE38CYBHP4ncwAuPeIgqTvZoD0fYIBD4rUrD8CpnzpACOAJ1+iegcJSA5yupxdDW7f3WOYNq95OWE+fe/CcpiKLNHHU6OYEH/7u7wsQs8C5ofpdVopkuweEBm+TjVwJBhKN+4Q4Bz/LMBJ5jqA71CL+cSHJasxDGWPVGY=",
        //编码格式
        'charset' => "UTF-8",
        //支付宝网关
        'gatewayUrl' => "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
//        'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
        //应用ID
        'app_id' => "2021000122668476",
        'notify_url' => WEB_URL."/alipay/alifacepay/alipayNotify",
        //同步跳转
        'return_url' => WEB_URL."",
        //最大查询重试次数
        'MaxQueryRetry' => "10",
        //查询间隔
        'QueryDuration' => "3"
    ];
    public function alipay(){
        $outTradeNo = time();
        $subject = '测试';
        $totalAmount = 1;
        $undiscountableAmount = "0.01";
        // 卖家支付宝账号ID，用于支持一个签约账号下支持打款到不同的收款账号，(打款到sellerId对应的支付宝账号)
        //$sellerId = "";
        $body = "购买商品2件共15.00元";
        //商户操作员编号，添加此参数可以为商户操作员做销售统计
        $operatorId = "";
        // (可选) 商户门店编号，通过门店号和商家后台可以配置精准到门店的折扣信息，详询支付宝技术支持
        $storeId = "";
        // 支付宝的店铺编号
        $alipayStoreId= "";
        $providerId = ""; //系统商pid,作为系统商返佣数据提取的依据
        $extendParams = new ExtendParams();
        $extendParams->setSysServiceProviderId($providerId);
        $extendParamsArr = $extendParams->getExtendParams();
        // 支付超时，线下扫码交易定义为5分钟
        $timeExpress = "5m";
        //第三方应用授权令牌,商户授权系统商开发模式下使用
        $appAuthToken = "";//根据真实值填写
        // 创建请求builder，设置请求参数
        $qrPayRequestBuilder = new AlipayTradePrecreateContentBuilder();
        $qrPayRequestBuilder->setOutTradeNo($outTradeNo);
        $qrPayRequestBuilder->setTotalAmount($totalAmount);
        $qrPayRequestBuilder->setTimeExpress($timeExpress);
        $qrPayRequestBuilder->setSubject($subject);
        $qrPayRequestBuilder->setBody($body);
        $qrPayRequestBuilder->setUndiscountableAmount($undiscountableAmount);
        $qrPayRequestBuilder->setExtendParams($extendParamsArr);
        $qrPayRequestBuilder->setStoreId($storeId);
        $qrPayRequestBuilder->setOperatorId($operatorId);
        $qrPayRequestBuilder->setAlipayStoreId($alipayStoreId);
        $qrPayRequestBuilder->setAppAuthToken($appAuthToken);
        // 调用qrPay方法获取当面付应答
        $qrPay = new AlipayTradeService($this->config);
//        halt($qrPayRequestBuilder);
        $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
        //	根据状态值进行业务处理
        switch ($qrPayResult->getTradeStatus()){
            case "SUCCESS":
                echo "支付宝创建订单二维码成功:"."<br>---------------------------------------<br>";
                $response = $qrPayResult->getResponse();
                $qrCode = new QrCode($response->qr_code);
                header('Content-Type: '.$qrCode->getContentType());
                $path='alifacepay/a.png';
                if (file_exists($path)){
                    unlink($path);
                }
                $qrCode->save($path);
//                echo $qrCode->getImage();
                $image = '<img src='.WEB_URL.'"/alifacepay/a.png"/>';
                return $image;
//                $qrcode = $qrPay->create_erweima($response->qr_code);
//                echo $qrcode;
                break;
            case "FAILED":
                echo "支付宝创建订单二维码失败!!!"."<br>--------------------------<br>";
                if(!empty($qrPayResult->getResponse())){
                    print_r($qrPayResult->getResponse());
                }
                break;
            case "UNKNOWN":
                echo "系统异常，状态未知!!!"."<br>--------------------------<br>";
                if(!empty($qrPayResult->getResponse())){
                    print_r($qrPayResult->getResponse());
                }
                break;
            default:
                echo "不支持的返回状态，创建订单二维码返回异常!!!";
                break;
        }
        return ;
    }

}